package com.hksj.app.adapter;

import com.hksj.base.service.DefaultDeviceService;
import com.hksj.base.vo.DeviceVO;
import com.hksj.common.quartz.adapter.DeviceInfoAdapter;
import com.hksj.common.quartz.vo.CronJobDeviceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component
public class CronJobDeviceInfoAdapter implements DeviceInfoAdapter {
    @Autowired
    private DefaultDeviceService deviceService;

    @Override
    public void wrapDeviceInfo(List<CronJobDeviceVO> deviceInfoList) {
        List<DeviceVO> deviceVOList = new ArrayList<>();
        DeviceVO tDeviceVO = null;
        if (!ObjectUtils.isEmpty(deviceInfoList)){
            for (CronJobDeviceVO cronJobDeviceVO : deviceInfoList) {
                tDeviceVO = new DeviceVO();
                tDeviceVO.setId(cronJobDeviceVO.getId().toString());
                tDeviceVO.setPaths(cronJobDeviceVO.getPaths());
                deviceVOList.add(tDeviceVO);
            }
            deviceService.wrapAddr(deviceVOList);
            Map<String, DeviceVO> deviceVOMap = deviceVOList.stream().collect(Collectors.toMap(DeviceVO::getId, Function.identity()));
            for (CronJobDeviceVO cronJobDeviceVO : deviceInfoList) {
               try{
                   DeviceVO deviceVO = deviceVOMap.get(cronJobDeviceVO.getId().toString());
                   if (deviceVO != null){
                       BeanUtils.copyProperties(deviceVO, cronJobDeviceVO);
                   }
               }catch (Exception e){
                   e.printStackTrace();
               }
            }
        }
    }
}
